/*
 * 
 * Matthew Cramer, Jesse Martinez, Marc Mendoza 
 * CSE 325 Lab 1
 * File: TestingInterface.c
 * Description: Manager that handles calls to the Queue.  This is called from
 *              the testing interface to perform the functions to the stack.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include "QueueManager.h"

#define NUM_REGS 3
#define MAX_PROCESSES 20


int main(int argc, char *argv[])
{
    /*
     * Read in test input from command line
     */
    if(argc != 2)
    {
        printf("Usage: Manager test_input_file\n");
        exit(0);
    }
    
    /*
     * Open File to Read test input for program
     */
    FILE *file = fopen ( argv[1], "r" );
    
    /*
     * Stores each line of the file
     */ 
    char lineString [128];
    
    int exitQueue = 0;
    while(exitQueue == 0)
    {
        int userInput;
        
        printf("\nQueue Manager: \n");
        printf("1) init_Q\n");
        printf("2) list\n");
        printf("3) enqueue\n");
        printf("4) dequeue\n");
        printf("5) delete\n");
        printf("6) exit\n");
        printf(": ");
        fgets(lineString, sizeof lineString, file);
        userInput = atoi(&lineString[0]); 
        printf("%d\n", userInput);
        //scanf("%d",&userInput);
        
        //init_Q
        if(userInput == 1)
        {
            clear();
            printf("\nQueue Initialized\n");
        }
        //list
        else if(userInput == 2)
        {
            int sizeOfQueue = QueueSize();
            if(sizeOfQueue == -1)
            {
                printf("\nError: No processes in Queue\n");
            }
            else
            {
                int temp[sizeOfQueue];
                list(&temp);
                int a;
                printf("\nQueue: \n"); 
                for(a=0; a<sizeOfQueue; a++)
                {
                    printf("%d\n", temp[a]);
                } 
            }
            
        }
        //enqueue
        else if(userInput == 3)
        {
            int sizeOfQueue = QueueSize();
            if(sizeOfQueue == MAX_PROCESSES)
            {
                printf("\nQueue is Full\n");
                /*
                 * Read in next 6 lines from file to remove from command list
                 */
                int temp=0;
                while(temp < 6)
                {
                    fgets(lineString, sizeof lineString, file);
                    temp++;
                }
            }
            else
            {
                int enqueueArray[6];
                printf("\nEnter in Information to Enqueue to Queue\n");
                printf("ProcessID Program_Status_Word Page_Table reg0 reg1 reg2\n");
            
                /*
                 * Read in next 6 lines of file to get struct data
                 */
                int temp=0;
                while(temp < 6)
                {
                    fgets(lineString, sizeof lineString, file);
                    enqueueArray[temp] = atoi(lineString); 
                    printf("%d\n",enqueueArray[temp]);
                    temp++;
                }
                
              /*   
                scanf("%d %d %d %d %d %d",&enqueueArray[0], 
                                          &enqueueArray[1], 
                                          &enqueueArray[2], 
                                          &enqueueArray[3], 
                                          &enqueueArray[4], 
                                          &enqueueArray[5]);
            */
                Enqueue(enqueueArray[0], 
                        enqueueArray[1], 
                        enqueueArray[2], 
                        enqueueArray[3], 
                        enqueueArray[4], 
                        enqueueArray[5]);
            }
        }
        //dequeue
        else if(userInput == 4)
        {
            int value;		
            value = Dequeue();
            if(value == -1)
            {
                printf("\nThere is nothing in the queue please enqueue first\n  ");
            }
            else
            {
                printf("\nThe Process Id is: %d\n", value);
            }
        }
        //delete
        else if(userInput == 5)
        {
            int value, pid, sizeOfQueue = QueueSize();
			
			//checks if queue is empty
            if(sizeOfQueue == -1)
            {
                printf("\nError: No processes in Queue\n");
            }
			else
			{
				printf("\nEnter in Information to Delete Process in Queue\n");
				printf("ProcessID\n");
				//scanf("%d",&pid);
				/*
				 * Get PID from text file input
				 */
				fgets(lineString, sizeof lineString, file);
				pid = atoi(lineString);
				printf("%d\n",pid);
			
				value = Delete(pid);
				if(value == -2)
				{
					printf("\nError: Process not found.\n");
				}
				else
				{
					printf("\nThe Process Id is: %d\n\n", value);
				}
			}
        }
        //Exit
        else if(userInput == 6)
        {
            exitQueue = 1;
        }
        //If input is not recognized
        else
        {
            printf("\nInput Not Recognized\n");
        }
    }
    /*
     * Close input file
     */
    fclose (file);
    return 0;
}
